﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CTMOES.DAL;
using CTMOES.Models;
using CTMOES.Helper;

namespace CTMAdmin
{
    public partial class ManagementForm : Form
    {
        public ManagementForm()
        {
            InitializeComponent();
        }

        private void btn_Get_Click(object sender, EventArgs e)
        {
            ShowData();
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            DataBox box = new DataBox();
            box.Show();
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dataProblem.CurrentRow;
            if (row != null)
            {
                DataBox box = new DataBox();
                box.mode = 1;

                QueModel model = GetModelFromRow(row);

                if (model != null)
                {
                    model.OptionList = GetListFromView(dataOption);
                    box.queModel = model;

                    box.Show();
                }
            }
            else
                MessageBox.Show("请选中一行");
        }

        private void btn_Del_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = null;

            switch (tabManagement.SelectedIndex)
            {
                case 0:
                    row = dataProblem.CurrentRow;
                    break;
                case 1:
                    row = dataJudgement.CurrentRow;
                    break;
            }

            if (row != null)
            {
                QueModel model = GetModelFromRow(row);
                DataHelper.DelQue(model.ID);
                ShowData();
                MessageBox.Show("删除成功！");
            }
            else
                MessageBox.Show("请选中一行");
        }

        private QueModel GetModelFromRow(DataGridViewRow row)
        {
            QueModel model = null;
            if (row != null)
            {
                model = new QueModel();
                model.ID = (Guid)row.Cells["ID"].Value;
                model.Category = (int)row.Cells["Category"].Value;
                model.ProContent = (string)row.Cells["ProContent"].Value;
                if (tabManagement.SelectedIndex == 0)
                    model.SubjectID = (Guid)row.Cells["SubjectID"].Value;
            }

            return model;
        }

        private List<OptionModel> GetListFromView(DataGridView view)
        {
            List<OptionModel> list = new List<OptionModel>();
            foreach (DataGridViewRow row in view.Rows)
            {
                list.Add(GetOModelFromRow(row));
            }

            return list;
        }

        private OptionModel GetOModelFromRow(DataGridViewRow row)
        {
            OptionModel model = new OptionModel();
            model.ID = (Guid)row.Cells[0].Value;
            model.QueID = (Guid)row.Cells[1].Value;
            model.Description = (string)row.Cells[2].Value;
            model.Correct = Convert.ToBoolean(row.Cells[3].Value);

            return model;
        }

        private void ShowData()
        {
            dataProblem.DataSource = null;
            switch (tabManagement.SelectedIndex)
            {
                case 0:
                    List<QueModel> list = QuestionProvider.GetQueList(0);
                    list.AddRange(QuestionProvider.GetQueList(1));
                    dataProblem.DataSource = list;
                    dataProblem.Columns["ID"].Visible = false;
                    dataProblem.Columns["Category"].Visible = false;
                    dataProblem.Columns["SubjectID"].Visible = false;
                    dataProblem.Columns["ProContent"].FillWeight = 80;
                    dataProblem.Columns["Subject"].FillWeight = 20;
                    break;
                case 1:
                    dataJudgement.DataSource = DataHelper.GetAllJudgment();
                    dataJudgement.Columns["ID"].Visible = false;
                    dataJudgement.Columns["Category"].Visible = false;
                    dataJudgement.Columns["ProContent"].FillWeight = 80;
                    dataJudgement.Columns["Correct"].FillWeight = 20;
                    dataJudgement.Columns["Name"].FillWeight = 20;
                    break;
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewRow row = dataProblem.CurrentRow;
            if (row != null)
            {
                dataOption.DataSource = OptionProvider.GetOptions(GetModelFromRow(row).ID);
                dataOption.Columns["ID"].Visible = false;
                dataOption.Columns["QueID"].Visible = false;
                dataOption.Columns["StuAns"].Visible = false;
                dataOption.Columns["Description"].FillWeight = 80;
                dataOption.Columns["Correct"].FillWeight = 20;
            }
        }
    }
}
